// src/main/java/cn/edu/tju/elm/controller/wallet/CreditController.java
package cn.edu.tju.elm.controller.wallet;

import cn.edu.tju.elm.model.CreditHistory;
import cn.edu.tju.elm.model.CreditRating;
import cn.edu.tju.elm.service.CreditService;
import cn.edu.tju.elm.service.VirtualWalletService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/api/wallet/credit")
@Tag(name = "credit-controller", description = "信用管理相关接口")
public class CreditController {

    @Autowired
    private CreditService creditService;

    @Autowired
    private VirtualWalletService walletService;

    /**
     * 检查是否为VIP用户
     */
    @GetMapping("/is-vip")
    @Operation(summary = "检查VIP状态", description = "检查用户是否为VIP用户")
    @ApiResponses({
        @ApiResponse(responseCode = "200", description = "获取成功")
    })
    public ResponseEntity<Boolean> isVipUser(
            @Parameter(description = "用户ID", required = true) @RequestParam Long userId) {
        try {
            boolean isVip = creditService.isVipUser(userId);
            return ResponseEntity.ok(isVip);
        } catch (Exception e) {
            return ResponseEntity.ok(false);
        }
    }

    /**
     * 获取信用评级
     */
    @GetMapping("/rating")
    @Operation(summary = "获取信用评级", description = "获取用户的信用评级")
    @ApiResponses({
        @ApiResponse(responseCode = "200", description = "获取成功"),
        @ApiResponse(responseCode = "400", description = "获取失败")
    })
    public ResponseEntity<CreditRating> getCreditRating(
            @Parameter(description = "用户ID", required = true) @RequestParam Long userId) {
        try {
            CreditRating rating = creditService.getCreditRating(userId);
            return ResponseEntity.ok(rating);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(null);
        }
    }

    /**
     * 获取已用信用额度
     */
    @GetMapping("/used-credit")
    @Operation(summary = "获取已用信用额度", description = "获取用户已使用的信用额度")
    @ApiResponses({
        @ApiResponse(responseCode = "200", description = "获取成功"),
        @ApiResponse(responseCode = "400", description = "获取失败")
    })
    public ResponseEntity<BigDecimal> getUsedCredit(
            @Parameter(description = "用户ID", required = true) @RequestParam Long userId) {
        try {
            BigDecimal usedCredit = creditService.getUsedCredit(userId);
            return ResponseEntity.ok(usedCredit);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(BigDecimal.ZERO);
        }
    }

    /**
     * 获取剩余信用额度
     */
    @GetMapping("/remaining-credit")
    @Operation(summary = "获取剩余信用额度", description = "获取用户剩余的信用额度")
    @ApiResponses({
        @ApiResponse(responseCode = "200", description = "获取成功"),
        @ApiResponse(responseCode = "400", description = "获取失败")
    })
    public ResponseEntity<BigDecimal> getRemainingCredit(
            @Parameter(description = "用户ID", required = true) @RequestParam Long userId) {
        try {
            BigDecimal remainingCredit = creditService.getRemainingCredit(userId);
            return ResponseEntity.ok(remainingCredit);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(BigDecimal.ZERO);
        }
    }

    /**
     * 检查是否逾期
     */
    @GetMapping("/is-overdue")
    @Operation(summary = "检查逾期状态", description = "检查用户是否逾期")
    @ApiResponses({
        @ApiResponse(responseCode = "200", description = "获取成功")
    })
    public ResponseEntity<Boolean> isOverdue(
            @Parameter(description = "用户ID", required = true) @RequestParam Long userId) {
        try {
            boolean isOverdue = creditService.isOverdue(userId);
            return ResponseEntity.ok(isOverdue);
        } catch (Exception e) {
            return ResponseEntity.ok(false);
        }
    }

    /**
     * 计算逾期利息
     */
    @GetMapping("/overdue-interest")
    @Operation(summary = "计算逾期利息", description = "计算用户的逾期利息")
    @ApiResponses({
        @ApiResponse(responseCode = "200", description = "计算成功"),
        @ApiResponse(responseCode = "400", description = "计算失败")
    })
    public ResponseEntity<BigDecimal> calculateOverdueInterest(
            @Parameter(description = "用户ID", required = true) @RequestParam Long userId) {
        try {
            BigDecimal interest = creditService.calculateOverdueInterest(userId);
            return ResponseEntity.ok(interest);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(BigDecimal.ZERO);
        }
    }

    /**
     * 获取信用历史记录
     */
    @GetMapping("/history")
    @Operation(summary = "获取信用历史", description = "获取用户的信用历史记录")
    @ApiResponses({
        @ApiResponse(responseCode = "200", description = "获取成功"),
        @ApiResponse(responseCode = "400", description = "获取失败")
    })
    public ResponseEntity<List<CreditHistory>> getCreditHistory(
            @Parameter(description = "用户ID", required = true) @RequestParam Long userId) {
        try {
            List<CreditHistory> history = creditService.getCreditHistory(userId);
            return ResponseEntity.ok(history);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(null);
        }
    }
}